package com.example.controller.stream;

import com.example.entity.Staff;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author ChenXiaoBin
 * on 2019-12-05
 */
@RestController
@RequestMapping("/stream/V1.0")
public class StreamController {

    public static void main(String[] arg) {
        List<Staff> staffs = new ArrayList<>();
        Staff staffOne = new Staff();
        staffOne.setName("陈晓斌");
        staffOne.setAge(18);

        Staff staffSecond = new Staff();
        staffSecond.setName("陈晓斌");
        staffSecond.setAge(8);

        Staff staffThread = new Staff();
        staffThread.setName("陈晓");
        staffThread.setAge(5);

        Staff staffFour = new Staff();
        staffFour.setName("陈晓斌");
        staffFour.setAge(18);

        List<String> names = new ArrayList<>();
        names.add("陈晓斌");
        names.add("陈12晓");

        staffs.add(staffOne);
        staffs.add(staffSecond);
        staffs.add(staffThread);
        staffs.add(staffFour);
        //生成流对象
        Stream<String> stream = Stream.of("chaimm", "peter", "john");
        //filter筛选转list集合 对原始 Stream 进行某项测试，通过测试的元素被留下来生成一个新 Stream
        List<String> fileString = stream.filter(x -> x.equals("john")).collect(Collectors.toList());
        //filter筛选转数组集合
        Staff[] staffLis = staffs.stream().filter(staff -> staff.getName().equals("陈晓斌")).toArray(Staff[]::new);
        //流去重
        staffs.stream().distinct().collect(Collectors.toList());
        //map映射
        staffs.stream().map(str -> str.getName()).collect(Collectors.toList());
        //是否匹配任一元素：anyMatch、 allMatch：Stream 中全部元素符合传入的 predicate，返回 true、
        // noneMatch：Stream 中没有一个元素符合传入的 predicate，返回 true
        staffs.stream().allMatch(str -> names.contains(str.getName()));
        //groupingBy/partitioningBy 分组
        Map<String, List<Staff>> collect = staffs.stream().collect(Collectors.groupingBy(str -> str.getName()));
        Map<Boolean, List<Staff>> other = staffs.stream().collect(Collectors.partitioningBy(str -> str.getAge() < 10));


    }

}
